from hep.aida import *

from java.awt import Color
from java.awt import Font
from java.lang import *
from java.util import *
from java.io import *

from jhplot.jadraw import *
from jhplot  import *

from fr.in2p3.ipnl.aidautilities import *
from fr.in2p3.ipnl.oflap.scint import *
from jscinttools..utilities.jhplotutilities import *

Locale.setDefault(Locale.ENGLISH)
System.getProperties().setProperty("hep.aida.BatchAnalysisFactory","hep.aida.ref.BatchAnalysisFactory")

showFigures = False
writeFigures = not showFigures

""" Define colors and fonts """
figType = [".png", ".eps"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.magenta, Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 20)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 15)

""" Define the Path and files names """
aidaPathL = "/home/marc-antoine/Documents/BGO/QFin"
aidaFileL = "ScintillationAnalysis_BGO"
aidaPathQ = "/home/marc-antoine/Documents/BGO/QFin/QueensData/queenstools"
aidaFileQ = "ScintillationAnalysis_BGO_QFile"
outPath = "/home/marc-antoine/Dropbox/BGO"
afrL = AidaFileReader(aidaPathL, aidaFileL)
afrQ = AidaFileReader(aidaPathQ, aidaFileQ)

""" Create the DPS with the 2 initials DPS and do a fit """
mainDecay = ScintUtil.fusionDPS(afrL.getDPS("/Kinetic/Decay_Comp#1"),afrQ.getDPS("/Kinetic/Decay_Q_Comp#2"))
kinParam = [133.33, 2.22, 74.27, 0.0077, 12.185]
theFitResult = ScintDataFitter.doKineticFit(mainDecay, kinParam)
"""
###############################################################
# Plotting Part
###############################################################
"""
""" Create the plottable JHep objects from AIDA IObjects """
""" The DataPointSet """
#mainP1D = P1D(mainDecay)
#mainP1D.setTitle("Main Component")
#mainP1D.setStyle("p")
#mainP1D.setSymbol(2)
#mainP1D.setSymbolSize(12)
#mainP1D.setColor(colors[0])
#mainP1D.setPenWidth(2)
#mainP1D.setErrY(true)
#mainP1D.setErrX(false)
#mainP1D.setErrColor(mainP1D.getColor())

mainP1DL = P1D(afrL.getDPS("/Kinetic/Decay_Comp#1"))
#mainP1DL.setTitle("Crystal 1")
mainP1DL.setTitle("BGO-L")
mainP1DL.setStyle("p")
mainP1DL.setSymbol(6)
mainP1DL.setSymbolSize(12)
mainP1DL.setColor(colors[0])
mainP1DL.setPenWidth(2)
mainP1DL.setErrY(True)
mainP1DL.setErrX(False)
mainP1DL.setErrColor(mainP1DL.getColor())

mainP1DQ = P1D(afrQ.getDPS("/Kinetic/Decay_Q_Comp#2"))
mainP1DQ.setTitle("BGO-Q")
#mainP1DQ.setTitle("Crystal 2")
mainP1DQ.setStyle("p")
mainP1DQ.setSymbol(8)
mainP1DQ.setSymbolSize(16)
mainP1DQ.setColor(colors[1])
mainP1DQ.setPenWidth(2)
mainP1DQ.setErrY(True)
mainP1DQ.setErrX(False)
mainP1DQ.setErrColor(mainP1DQ.getColor())

""" The gamma fit fuction """
mainF1D = F1D(theFitResult.fittedFunction(), 0.1, 1000)
mainF1D.setTitle("Fit")
mainF1D.setPoints(10000)
mainF1D.setColor(colors[2])
mainF1D.setPenWidth(2)

""" The alpha fit Function """
alphaF1D = F1D(afrL.getFunc("/Kinetic/Fit Function alpha"), 0.1, 1000)
alphaF1D.setTitle("Fit &alpha; (Gironnet et al.)")
alphaF1D.setPoints(10000)
alphaF1D.setColor(colors[4])
alphaF1D.setPenWidth(2)
alphaF1D.setPenDash()

# Create the JAKeys and their labels for the legend
#mainP1DKey = JaKey(mainP1D.getSymbol(), mainP1D.getTitle(), 0.20, 0.65, "NDC")
#mainP1DKey.setColor(mainP1D.getColor())
#mainP1DKey.setKeyColor(mainP1D.getColor())
#mainP1DKey.setPenWidth(2)
#mainP1DKey.setFont(legendFont)

mainP1DKeyL = JaKey(mainP1DL.getSymbol(), mainP1DL.getTitle(), 0.20, 0.65, "NDC")
mainP1DKeyL.setColor(mainP1DL.getColor())
mainP1DKeyL.setKeyColor(mainP1DL.getColor())
mainP1DKeyL.setPenWidth(2)
mainP1DKeyL.setFont(legendFont)

mainP1DKeyQ = JaKey(mainP1DQ.getSymbol(), mainP1DQ.getTitle(), 0.20, 0.70, "NDC")
mainP1DKeyQ.setColor(mainP1DQ.getColor())
mainP1DKeyQ.setKeyColor(mainP1DQ.getColor())
mainP1DKeyQ.setPenWidth(2)
mainP1DKeyQ.setFont(legendFont)

mainF1DKey = JaKey(20, mainF1D.getTitle(), 0.20, 0.75, "NDC")
mainF1DKey.setColor(mainF1D.getColor())
mainF1DKey.setKeyColor(mainF1D.getColor())
mainF1DKey.setPenWidth(2)
mainF1DKey.setFont(legendFont)

alphaF1DKey = JaKey(20, alphaF1D.getTitle(), 0.20, 0.80, "NDC")
alphaF1DKey.setColor(alphaF1D.getColor())
alphaF1DKey.setKeyColor(alphaF1D.getColor())
alphaF1DKey.setPenWidth(2)
alphaF1DKey.setFont(legendFont)

alphaRef = JaText("(Gironnet et al. Nucl. Instrum. Methods Phys. Res. A 594, 358 (2008))", 0.20, 0.82, "NDC")
alphaRef.setColor(alphaF1D.getColor())
alphaRef.setFont(Font(Font.SERIF, Font.PLAIN, 12))

""" Create the Plotter """
mainPlotter = HPlotJa("Fusion Kinetics", 600, 600, 1, 1, True)
mainPlotter.setGTitle("Main Scintillation Component of BGO", titleFont)
mainPlotter.setAntiAlias(False)
if showFigures:
	mainPlotter.visible(True)
else:
	mainPlotter.visible(False)
mainPlotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
mainPlotter.setRange(1, 1000, 0.1, 1000)
mainPlotter.setLogScale(0, True)
mainPlotter.setLogScale(1, True)
mainPlotter.setTicFont(ticFont)
mainPlotter.setShowStatBox(False)
mainPlotter.showKey(False)
mainPlotter.setNameX("Temperature (K)")
mainPlotter.setNameY("Decay Time (&micro;s)")

""" Plots all the JHep objects """
mainPlotter.draw(mainF1D)
mainPlotter.draw(alphaF1D)
#mainPlotter.draw(mainP1D)
mainPlotter.draw(mainP1DL)
mainPlotter.draw(mainP1DQ)
mainPlotter.draw(mainP1DKeyL)
mainPlotter.draw(mainP1DKeyQ)
mainPlotter.draw(mainF1DKey)
mainPlotter.draw(alphaF1DKey)
#mainPlotter.draw(alphaRef)

""" Modifies the JaAxes and the axes labels """
labX = mainPlotter.getLabelX()[0][0]
labX.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, labX, mainPlotter)
labY = mainPlotter.getLabelY()[0][0]
labY.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, labY, mainPlotter)

theAxes = mainPlotter.getPad()
theAxes.setLabelSpace(0, 0.0)
theAxes.setLabelSpace(1, -0.025)
HPlotJaUtil.setTickLabelOneToPower(theAxes);

mainPlotter.update()

""" Saves or not the plot """
if writeFigures:
	for i in range(len(figType)):
		outFileName = "FusKin_2V5" + figType[i]
		mainPlotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
		
mainPlotter.update()
print("That's all folks !")

